﻿using Microsoft.Maps.MapControl.WPF;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using TrailMapManager.Common;
using TrailMapManager.Communication;

namespace TrailMapManager.Modules.Maps.Logic
{
    public class MapsModuleMainViewLogic : BaseViewLogic
    {
        public CommunicationManager CommMan
        {
            get { return CommunicationManager.Instance; }
        }

        private RelayCommand _addMapCommand;
        public RelayCommand AddMapCommand
        {
            get
            {
                if (_addMapCommand == null)
                {
                    _addMapCommand = new RelayCommand(
                        () =>
                        {
                            AddMap();
                        });
                }
                return _addMapCommand;
            }
        }

        private RelayCommand _deleteMapCommand;
        public RelayCommand DeleteMapCommand
        {
            get
            {
                if (_deleteMapCommand == null)
                {
                    _deleteMapCommand = new RelayCommand(
                        () =>
                        {
                            DeleteMap();
                        },
                        () =>
                        {
                            return SelectedMap != null;
                        });
                }
                return _deleteMapCommand;
            }
        }

        private RelayCommand _deleteAllMapCommand;
        public RelayCommand DeleteAllMapCommand
        {
            get
            {
                if (_deleteAllMapCommand == null)
                {
                    _deleteAllMapCommand = new RelayCommand(
                        () =>
                        {
                            DeleteAllMap();
                        },
                        () =>
                        {
                            if (CommMan.Maps != null)
                            {
                                return CommMan.Maps.Count > 0;
                            }
                            return false;
                        });
                }
                return _deleteAllMapCommand;
            }
        }


        private Location _mapCenter;
        public Location MapCenter
        {
            get { return _mapCenter; }
            set { _mapCenter = value; NotifyPropertyChanged("MapCenter"); }
        }

        private MapInformation _selectedMap;
        public MapInformation SelectedMap
        {
            get { return _selectedMap; }
            set { _selectedMap = value; NotifyPropertyChanged("SelectedMap"); NotifyPropertyChanged("MapBoundaryLocation"); MoveCenterLittleBit(); }
        }

        public LocationCollection MapBoundaryLocation
        {
            get
            {
                var locations = new LocationCollection();
                if (SelectedMap != null)
                {
                    locations.Add(new Location() { Longitude = SelectedMap.Boundary.NorthWest.Longitude, Latitude = SelectedMap.Boundary.NorthWest.Latitude });
                    locations.Add(new Location() { Longitude = SelectedMap.Boundary.SoutEast.Longitude, Latitude = SelectedMap.Boundary.NorthWest.Latitude });
                    locations.Add(new Location() { Longitude = SelectedMap.Boundary.SoutEast.Longitude, Latitude = SelectedMap.Boundary.SoutEast.Latitude });
                    locations.Add(new Location() { Longitude = SelectedMap.Boundary.NorthWest.Longitude, Latitude = SelectedMap.Boundary.SoutEast.Latitude });
                    
                }

                return locations;
            }
        }

        public MapsModuleMainViewLogic()
        {
            MapCenter = new Location();
        }

        private void MoveCenterLittleBit()
        {
            new Task(
                () =>
                {
                    var oldCenter = MapCenter;
                    var newCenter = new Location(MapCenter.Latitude, MapCenter.Longitude + 0.00000000001);
                    InvokeOnMainThread(
                        () =>
                        {
                            MapCenter = newCenter;
                        });
                    Thread.Sleep(10);
                    InvokeOnMainThread(
                        () =>
                        {
                            MapCenter = oldCenter;
                        });
                }).Start();
        }


        private void AddMap()
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();

            openFileDialog.Multiselect = true;
            openFileDialog.Filter = "All Image Files | *.img; *.hgt";

            if (openFileDialog.ShowDialog().Value)
            {
                foreach (var fileName in openFileDialog.FileNames)
                {
                    CommunicationManager.Instance.AddMapToUpload(fileName);
                }
                

            }

        }

        private void DeleteMap()
        {
            CommMan.AddNewCommandToQueue(new DeleteTrailMapCommand()
            {
                Map = SelectedMap,
                Type = CommandType.DeleteMap,
            });
        }

        private void DeleteAllMap()
        {
            foreach (var map in CommMan.Maps)
            {
                CommMan.AddNewCommandToQueue(new DeleteTrailMapCommand()
                {
                    Map = map,
                    Type = CommandType.DeleteMap,
                });
            }
        }
    }
}
